package com.swpu.controller;

import com.swpu.exception.BusinessException;
import com.swpu.model.dto.CaptchaResult;
import com.swpu.model.entity.SysUser;
import com.swpu.model.query.UserRegisterQuery;
import com.swpu.result.Result;
import com.swpu.result.ResultCode;
import com.swpu.service.SysUserService;
import com.swpu.utils.RSAUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestController {
    @Resource
    private SysUserService sysUserService;


    /**
     * @param :
     * @return Result<CaptchaResult>
     * @author 宋建博
     * @description 获取验证码
     * @date 2024/1/10 16:50
     */
    @GetMapping("/captcha")
    public Result<CaptchaResult> test2() {
        CaptchaResult captcha = sysUserService.getCaptcha();
        return Result.success(captcha);
    }
    /**
     * @param username:
     * @param password:
     * @return Result<String>
     * @author 宋建博
     * @description 登录
     * @date 2024/1/10 16:49
     */
    @PostMapping("/login")
    public Result<String> login(@RequestParam String username, @RequestParam String password) {
        String token =sysUserService.login(username,password);
        return Result.success(token);
    }

    /**
     * @param userId:
     * @return Result
     * @author 宋建博
     * @description 刷新token
     * @date 2024/1/10 16:51
     */

    @PostMapping("/refreshToken")
    public Result refreshToken(@RequestParam String userId) {
        String token = sysUserService.refreshToken(userId);
        return Result.success(token);
    }
    /**
     * @param userRegisterQuery:
     * @return Result
     * @author 宋建博
     * @description 注册
     * @date 2024/1/10 16:51
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserRegisterQuery userRegisterQuery) {
        sysUserService.register(userRegisterQuery);
        return Result.success();
    }

    @PostMapping("/test")
    public Result<String> test(@RequestParam String username, @RequestParam String password) {
        String token =sysUserService.login(username,password);
        return Result.success(token);
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        // TODO: 2024/1/12 完善接口  通过请求头信息获取用户id
        return Result.success();
    }



}
